package com.carrie.config.handler;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author XuHaiLong
 * @since 2025/4/15 13:25
 */
public class MySessionInformationExpireStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",-1);
        map.put("msg","当前账户已在别的设备登");
        HttpServletResponse httpServletResponse = (HttpServletResponse) event.getResponse();
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(map));

    }
}
